import $ from 'jquery'
import VuePerfectScrollbar from "vue-perfect-scrollbar";

export default {

  install(Vue, options) {
    /**
     * 切換服務器 139、119、118、123
     * @type {number}
     * @private
     */
    Vue.prototype._SERVER =
      typeof location.origin === 'undefined' && !location.host.match(/localhost/g) ?
        location.host.replace(/[\.]/g, '') :
        typeof location.origin !== 'undefined' && !location.host.match(/localhost/g) ?
          location.hostname.replace(/[\.]/g, '') :
          17216646;

    Vue.prototype._SERVER_LIST = {
      // 139199778: {
      //   url     : 'http://139.199.7.78',
      //   title   : '测试服务器',
      //   logo    : 'icon-logojibiaotiwenzi',
      //   miniLogo: 'icon-dandulogo'
      // },
      // 1192923169: {
      //   url     : 'http://119.29.231.69',
      //   title   : '坪山智慧经服',
      //   logo    : 'icon-logojibiaotiwenzi',
      //   miniLogo: 'icon-dandulogo'
      // },
      // 1232078650: {
      //   url     : 'http://123.207.86.50',
      //   title   : '龙岗区产业管理服务平台',
      //   logo    : require('../../../static/123/logo123.png'),
      //   miniLogo: require('../../../static/123/miniLogo123.png')
      // },
      // 2039136116: {
      //   url     : 'http://203.91.36.116',
      //   title   : '龙岗区产业管理服务平台',
      //   logo    : require('../../../static/123/logo123.png'),
      //   miniLogo: require('../../../static/123/miniLogo123.png')
      // },
      11812695244: {
        url     : 'http://118.126.95.244',
        title   : '坪山区科技创新专项资金申报系统',
        logo    : require('../../../static/118/logo118.png'),
        miniLogo: require('../../../static/118/miniLogo118.png')
      },
      120236189: {
        url     : 'http://120.236.1.89',  //118对应的120正式服
        title   : '坪山区科技创新专项资金申报系统',
        logo    : 'icon-logojibiaotiwenzi',
        miniLogo: 'icon-dandulogo'
      },
      17216646: {
        url     : 'http://172.16.6.46',  //118对应的内网测试服
        title   : '坪山区科技创新专项资金申报系统',
        logo    : 'icon-logojibiaotiwenzi',
        miniLogo: 'icon-dandulogo'
      },
      // 100068: {
      //   url     : 'http://10.0.0.68',
      //   title   : '龙岗区产业管理服务平台1.0',  // 龙岗内网测试机
      //   logo    : require('../../../static/123/logo123.png'),
      //   miniLogo: require('../../../static/123/miniLogo123.png')
      // },
      // 100058: {
      //   url     : 'http://10.0.0.58',
      //   title   : '坪山区科技创新专项资金申报系统', // 坪山科创内网测试机
      //   logo    : require('../../../static/118/logo118.png'),
      //   miniLogo: require('../../../static/118/miniLogo118.png')
      // },
    };

    Vue.prototype._SERVEROBJ = Vue.prototype._SERVER_LIST[Vue.prototype._SERVER];

    Vue.prototype._URL_v2 =
      typeof location.origin === 'undefined' && !location.host.match(/localhost/g) ? location.protocol + '//' + location.host + ':8080/zhjf-v2'
        : typeof location.origin !== 'undefined' && !location.host.match(/localhost/g) ? window.location.origin + ':8080/zhjf-v2'
        : `${Vue.prototype._SERVEROBJ.url}:8080/zhjf-v2`;


    Vue.prototype.getUserMsg = function () {
      let userMsg = this.$store.state.userMsg;
      let userId  = userMsg.loginName;
      return {
        userMsg,
        userId
      }
    }
    Vue.prototype.ajaxData   = function (obj, callBlack) {
      if (!window.sessionStorage.getItem('ZHJF')) {
        sessionStorage.clear()
        this.$router.push({path: '/login'})
        return false;
      }
      let url         = this._URL_v2 + obj.url,
          contentType = obj.contentType ? 'application/json; charset=UTF-8' : 'application/x-www-form-urlencoded; charset=UTF-8',
          $data       = obj.contentType ? JSON.stringify(obj.data) : obj.data;
      $.ajax({
        url        : url,
        type       : obj.type,
        async      : obj.async !== undefined ? false : true,
        xhrFields  : {
          withCredentials: true
        },
        data       : $data,
        dataType   : 'json',
        contentType: contentType,
        complete   : (xhr, status) => {
          var code    = xhr.getResponseHeader('code'),
              message = decodeURIComponent(xhr.getResponseHeader('message'));
          if (code == '401101' || code == '401121' || code == '401111' || xhr.status == 401) {
            callBlack && callBlack('', {
              code   : code,
              message: message
            });
            sessionStorage.clear()
            this.$router.push({path: '/login'})
            return false;
          }
          if (xhr.status == 200) {
            let res = xhr.responseJSON;
            if (res.code !== 0) {
              this.$Notice.error({
                // title: '错误',
                desc: res.message
              });
            } else {
              callBlack && callBlack(res.result, res);
            }
          } else {
            this.$Notice.error({
              desc: message
            });
            callBlack && callBlack('', {
              code   : code,
              message: message
            });
          }
        }
      })
    }

    Vue.component('VuePerfectScrollbar', VuePerfectScrollbar);

    Vue.prototype.getNowFormatDate    = function (h, m) {
      return (h < 10 ? '0' + h : h) + ':' + (m < 10 ? '0' + m : m);
    }
    Vue.prototype.getTimeAlert        = function (start, end) {
      var date = new Date();
      var $s   = (date.valueOf() - start) / (60 * 1000);
      var str  = '';
      str      = $s <= 1 ? '刚刚' : $s > 1 && $s <= 60 ? parseInt($s) + '分钟前' : '';
      if ($s > 60) {
        $s  = parseInt($s / 60);
        str = $s + '小时前';
        if ($s > 24) {
          $s  = $s / 24;
          str = parseInt($s) + '天前';
          if ($s > 30) {
            $s  = $s / 30;
            str = parseInt($s) + '月前'
          }
        }
      }
      return str;
    }
    Vue.prototype.getTime             = function (date, w) {
      if (date) {
        var dateNum = new Date(date),
            ymd     = dateNum.toLocaleString().match(/\d+\/\d+\/\d+/)[0] || '',
            h       = dateNum.getHours(),
            m       = dateNum.getMinutes(),
            hm      = this.getNowFormatDate(h, m);
        return !w ? ymd + ' ' + hm : w == 1 ? ymd : w == 2 ? hm : '';
      }
    }
    Vue.prototype.applyStatusGroup    = function (num) {
      let [a, b, c] = [[20, 21, 22, 23], [30, 31, 38, 39], [29, 40, 41, 42, 49]];
      return a.includes(num) ? '待处理' : b.includes(num) ? (this._SERVER == 123 ? '通过网上预受理' : '已通过线上审核') : c.includes(num) ? '已中断' : ''
    }
    Vue.prototype.applyStatusStr      = function (num, param, processInstance) {

      const status = {
        applyStatus20: {action: 'unread', str: '未阅读'},
        applyStatus21: {action: 'unprocess', str: '尚未处理'},
        applyStatus22: {action: 'consider', str: '感兴趣'},
        applyStatus23: {action: 'unconsider', str: '不考虑'},
        applyStatus29: {action: 'rejectOnline', str: '已驳回线上申请'},
        applyStatus30: {action: 'approveOnline', str: this._SERVER == 123 ? '' : '尚未确定办理时间'},
        applyStatus31: {action: 'appoint', str: this._SERVER == 123 ? '' : '已预约办理'},
        applyStatus38: {action: 'approveOffline', str: '确认拨款'},
        applyStatus39: {action: 'rejectOffline', str: '未通过'},
        applyStatus40: {action: '', str: '政策自动过期'},
        applyStatus41: {action: '', str: '政策强行下架'},
        applyStatus42: {action: 'block', str: '企业被拉黑'},
        applyStatus49: {action: 'terminate', str: '强制中断'}
      };
      if (!processInstance) {
        return status[`applyStatus${num}`][param ? 'action' : 'str']
      } else {
        if (num == 49) {
          return ''
        }
        let activities = processInstance.activities.length
        return activities == 1 ? '未开始' : activities == 2 ? '进行中' : '审核完毕'
      }
    }
    Vue.prototype.applyStatusOnOffStr = function (num, processInstance) {
      let [a, b] = [[20, 21, 22, 23, 29, 30, 31, 38, 39], [40, 41, 42, 49]];
      if (!processInstance) {
        return a.includes(num) ? '线上审核' : b.includes(num) ? '强行中断' : ''
      } else {
        return num == 49 ? '强行中断' : '线下审核'
      }
    }
    Vue.prototype.applyStatusNum      = function (obj) {
      let $obj = {
        unprocess     : 21,
        consider      : 22,
        unconsider    : 23,
        rejectOnline  : 29,
        approveOnline : 30,
        appoint       : 31,
        rejectOffline : 39,
        approveOffline: 38,
        terminate     : 49,
      }
      return $obj[obj]
    }
    Vue.prototype.comp                = function (a, b) {
      return a.appointDate - b.appointDate;
    }
    Vue.prototype.GoBack              = function () {
      this.$router.go(-1)
    }

    // 修复 iview在IE9/10报TypeError: 无法获取未定义或 null 引用的属性transfer
    var isIE = function () {
      return (window.navigator.userAgent.indexOf("MSIE") >= 1);
    }

    // 修复IE10及以下版本不支持dataset属性的问题，兼容transfer-dom.js中使用了dataset的问题
    if (isIE() && window.HTMLElement) {
      if (Object.getOwnPropertyNames(HTMLElement.prototype).indexOf('dataset') === -1) {
        Object.defineProperty(HTMLElement.prototype, 'dataset', {
          get: function () {
            var attributes = this.attributes;
            var name = [],
              value = [];
            var obj = {};
            for (var i = 0; i < attributes.length; i++) {
              if (attributes[i].nodeName.slice(0, 5) == 'data-') {
                name.push(attributes[i].nodeName.slice(5));
                value.push(attributes[i].nodeValue);
              }
            }
            for (var j = 0; j < name.length; j++) {
              obj[name[j]] = value[j];
            }
            return obj;
          }
        });
      }
    }
  }
}


